using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gko.Utils
{
	/// <summary>
	/// Extensions to <see cref="T:Gko.Utils.UnmanagedMemoryBlock"/>
	/// </summary>
	public static class MemoryBlockExtensions
	{
		/// <summary>
		/// Converts <see cref="T:System.IntPtr"/> to the memory block.
		/// </summary>
		/// <param name="ptr">The pointer to memory.</param>
		/// <param name="bytesCount">The bytes count.</param>
		/// <param name="disposeAction">The dispose action.</param>
		/// <returns>Memory block</returns>
		public static UnmanagedMemoryBlock ToMemoryBlock(this IntPtr ptr, int bytesCount, Action<IntPtr> disposeAction)
		{
			return new UnmanagedMemoryBlock(ptr, bytesCount, disposeAction);
		}

		/// <summary>
		/// Converts <see cref="T:System.IntPtr"/> to the memory block.
		/// </summary>
		/// <param name="ptr">The pointer to memory.</param>
		/// <param name="disposeAction">The dispose action.</param>
		/// <returns>Memory block</returns>
		public static UnmanagedMemoryBlock ToMemoryBlock(this IntPtr ptr, Action<IntPtr> disposeAction)
		{
			return new UnmanagedMemoryBlock(ptr, 0, disposeAction);
		}

		/// <summary>
		/// Converts <see cref="T:System.IntPtr"/> to the memory block.
		/// </summary>
		/// <param name="ptr">The pointer to memory.</param>
		/// <returns>Memory block</returns>
		public static UnmanagedMemoryBlock ToMemoryBlock(this IntPtr ptr)
		{
			return new UnmanagedMemoryBlock(ptr, 0, DummyDispose);
		}
		/// <summary>
		/// Converts array of bytes to the memory block.
		/// </summary>
		/// <param name="bytes">The bytes.</param>
		/// <returns>Memory block</returns>
		public static UnmanagedMemoryBlock ToMemoryBlock(this byte[] bytes)
		{
			return UnmanagedMemoryBlock.FromBytes(bytes);
		}

		private static void DummyDispose(IntPtr arg)
		{
		}
	}
}